<!--
 * @Author: lihaogn
 * @Date: 2021-12-06 00:17:21
 * @LastEditTime: 2021-12-08 14:39:51
 * @LastEditor: lihaogn
 * @Description: 重命名对话框
 * @FilePath: \lix-admin-vue2\src\views\system\user-manage\user-list\modules\RenameDialog.vue
-->
<template>
  <!-- 重命名对话框 start label-width="100px"-->
  <el-dialog
    :close-on-click-modal="false"
    title="重命名"
    :visible.sync="dialogVisible"
    width="420px"
    top="30vh"
    @close="handleDialogCancel('renameForm')"
  >
    <el-form
      ref="renameForm"
      :model="renameForm"
      :rules="renameFormRules"
      :inline="true"
    >
      <el-form-item
        label="用户名"
        prop="username"
        style="display: flex; margin: 0; width: 100%"
      >
        <el-input
          type="text"
          v-model.trim="renameForm.username"
          autocomplete="off"
          :autofocus="true"
          :show-word-limit="true"
          :minlength="3"
          :maxlength="20"
          clearable
          v-on:keyup.enter.native="handleRenameDialogOk('renameForm')"
        ></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button
        type="primary"
        :loading="okButtonLoading"
        @click="handleRenameDialogOk('renameForm')"
      >
        确 定</el-button
      >
    </span>
  </el-dialog>
  <!-- 重命名对话框 end-->
</template>

<script>
import { reqEditUser } from '../../../../../api/user-manage/user/api'
import { dialogMixin } from '../../../../../mixins/dialog'

export default {
  mixins: [dialogMixin],
  props: {
    oldName: {
      type: String,
      required: true
    },
    editId: {
      type: String,
      required: true
    }
  },
  data() {
    // 定义重命名时，原名称与修改后名称是否相同
    let validateRename = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'))
      } else {
        if (value === this.oldName) {
          callback(new Error('名称与原名称相同，请重新输入'))
        } else {
          callback()
        }
      }
    }
    return {
      renameForm: {
        username: ''
      },
      renameFormRules: {
        username: [
          { validator: validateRename, trigger: 'blur' },
          { min: 3, max: 20, message: '长度在3-20个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.dialogVisible = val
        this.renameForm.username = this.oldName
      }
    }
  },
  methods: {
    /**
     * @description: 重命名对话框取消事件
     * @param {*} formName
     * @return {*}
     */
    handleDialogCancel(formName) {
      this.$emit('update:visible', false)
      this.$refs[formName].resetFields()
    },
    /**
     * @description: 重命名对话框确认事件
     * @param {*}
     * @return {*}
     */
    handleRenameDialogOk(formName) {
      this.okButtonLoading = true
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const res = await reqEditUser(this.editId, {
            username: this.renameForm.username,
            type: 'rename'
          })
          if (res.success) {
            this.$message.success(res.message)
            this.okButtonLoading = false
            this.dialogVisible = false

            this.$emit('ok')
          } else {
            this.okButtonLoading = false
          }
          this.okButtonLoading = false
        } else {
          this.okButtonLoading = false
          return false
        }
      })
    }
  }
}
</script>

<style></style>
